//
//  GLESTexture.m
//  OpenGLES_03
//
//  Created by Keisuke Hata on 09/12/24.
//  Copyright 2009 Keisuke Hata. All rights reserved.
//

#import "GLESSprite.h"
#import "UIGLESSettingViewController.h"

@implementation EAGLSpriteBase

@synthesize hidden = _hidden;

- (void) draw
{
}

- (void) calc
{
}

@end

@implementation EAGLSprite2D

@synthesize pos = _pos , alpha = _alpha , angleZ = _angleZ , scale = _scale , anchor = _anchor;
@synthesize texture = _tex , tag = _tag;
@dynamic pvr;
@dynamic hasMipmap;

- (BOOL) pvr {
	
	return _tex.pvr;
}

- (BOOL) hasMipmap {
	
	return _tex.hasMipmap;
}

-(void) initParams {
	
	_pos		= CGPointMake(0.0f,0.0f);
	_alpha		= 1.0f;
	_angleZ		= 0.0f;
	_scale		= CGSizeMake(1.0f, 1.0f);
	_anchor		= CGPointMake(_tex.width/2, _tex.height/2);

	// PVR形式の場合
	if( self.pvr ) {
		_blend_s = GL_SRC_ALPHA;
		_blend_d = GL_ONE_MINUS_SRC_ALPHA;

	// PVR形式以外の場合
	}else {
		_blend_s = GL_ONE;
		_blend_d = GL_ONE_MINUS_SRC_ALPHA;
	}
	_magfilter	= GL_NEAREST;
	_minfilter	= GL_NEAREST;
	_wrap_s		= GL_CLAMP_TO_EDGE;
	_wrap_t		= GL_CLAMP_TO_EDGE;
	_offs		= CGPointMake(0.0f, 0.0f);
	_tag		= 0;
}

-(id) initWithFile : (NSString *)path {
	
	if( self = [super init] ) {
		_tex = [[Texture2D alloc] initWithContentsOfFile:path];
		if( !_tex )
			return nil;
		[self initParams];
		[self setup];
	}
	return self;
}

- (id) initWithFile: (NSString *)path objTag:(GLuint)tag {
	if( ![self initWithFile:path] ) {
		[self release];
		return nil;
	}
	_tag = tag;
	return self;
}

-(id) initWithPVRFile : (NSString *)path {
	if( self = [super init] )
	{
		_tex = [[Texture2D alloc] initWithContentsOfPVRTCFile:path];
		if( !_tex )
			return nil;
		[self initParams];
		[self setup];
	}
	return self;
}

- (id) initWithPVRFile: (NSString *)path objTag:(GLuint)tag {
	if( ![self initWithPVRFile:path] ) {
		[self release];
		return nil;
	}
	_tag = tag;
	return self;
}

-(id) initWithTexture : (Texture2D *)tex {
	if( self = [super init] )
	{
		self.texture = tex;
		[self initParams];
		[self setup];
	}
	return self;
}

- (id) initWithTexture : (Texture2D *)tex objTag:(GLuint)tag {
	if( ![self initWithTexture:tex] ) {
		[self release];
		return nil;
	}
	_tag = tag;
	return self;
}

- (id) initWithText : (NSString *)text 
		   fontColor:(UIColor *)color
	   fontAlignment:(UITextAlignment)alignment 
			fontName:(NSString *)name
			fontSize:(CGFloat)size; {

	if( self = [super init] ) {
		_tex = [[Texture2D alloc] initWithColorText:text 
										fontColor:color 
										fontAlignment:alignment 
										fontName:name 
										fontSize:size];
		if( !_tex )
			return nil;
	}
	[self initParams];
	[self setup];
		
	return self;
}

- (id) initWithText : (NSString *)text 
		   fontColor:(UIColor *)color
	   fontAlignment:(UITextAlignment)alignment 
			fontName:(NSString *)name
			fontSize:(CGFloat)size
			  objTag:(GLuint)tag {
	
	if( ![self initWithText:text 
			fontColor:color 
			fontAlignment:alignment 
			fontName:name 
			fontSize:size] ) {
		[self release];
		return nil;
	}
	[self initParams];
	[self setup];
	_tag = tag;

	return self;
}


- (void) setup {
}

- (void) draw {
	
	glPushMatrix();

	// 後ろから順にマトリクスが適用されます。
	// 拡大縮小→回転→移動
	glTranslatef( _pos.x, _pos.y, 0.0f );
	glRotatef( _angleZ, 0.0f, 0.0f, 1.0f);
	glScalef( _scale.width, _scale.height, 1.0f);

	// ①テクスチャの利用を有効にします。
	glEnable( GL_TEXTURE_2D );

	// ②テクスチャのブレンドを有効にします。
	glEnable( GL_BLEND );
	
	// ③テクスチャを関連付けます。
	[TextureUtils bindTexture2D:_tex];
	
	// ④テクスチャの各種設定します。
	[TextureUtils setTextureBlend:_blend_s blendDst:_blend_d];
	[TextureUtils setTextureMagFilter:_magfilter];
	[TextureUtils setTextureMinFilter:_minfilter];
	[TextureUtils setTextureWrapS:_wrap_s];
	[TextureUtils setTextureWrapT:_wrap_t];
	[TextureUtils setTextureUVOffset:CGPointMake(_offs.x,_offs.y)];

	// ⑤テクスチャに合わせたポリゴンを生成し、UVを指定して描画します。
	[DrawUtils drawWithTexture2D:_pos 
					imageAnchor:_anchor 
					imageAngleZ:_angleZ 
					imageAlpha:_alpha 
					imageScale:_scale 
					texture:_tex 
					isPVRTC:self.pvr];

	glDisable( GL_TEXTURE_2D );
	glDisable( GL_BLEND );
	
	glPopMatrix();
}

-(void) dealloc {
	[_tex release];
	[super dealloc];
}

@end

@implementation EAGLSprite3D

@synthesize vec = _vec , alpha = _alpha , rotate = _rot , scale = _scale , anchor = _anchor;
@synthesize texture = _tex , tag = _tag;
@dynamic pvr;

- (BOOL) pvr {
	
	return _tex.pvr;
}

- (BOOL) hasMipmap {
	
	return _tex.hasMipmap;
}

- (void) setup {
}

-(void) initParams {
	
	_vec = GLVector3DMake(0.0f, 0.0f, 0.0f);
	_rot = GLVector3DMake(0.0f, 0.0f, 0.0f);
	_anchor = GLVector3DMake(_tex.realwidth/2, _tex.realheight/2 , 0.0f );
	_alpha = 1.0f;
	_scale = CGSizeMake(1.0f, 1.0f);
	if( self.pvr ) {
		_blend_s = GL_SRC_ALPHA;
		_blend_d = GL_ONE_MINUS_SRC_ALPHA;
	}else {
		_blend_s = GL_ONE;
		_blend_d = GL_ONE_MINUS_SRC_ALPHA;
	}
	_magfilter = GL_NEAREST;
	_minfilter = GL_NEAREST;
	_wrap_s = GL_CLAMP_TO_EDGE;
	_wrap_t = GL_CLAMP_TO_EDGE;
	_offs = CGPointMake(0.0f, 0.0f);
	_tag = 0;
}

-(id) initWithContentsOfFile : (NSString *)path {

	if( self = [super init] ) {
		
		_tex = [[Texture2D alloc] initWithContentsOfFile:path];
		[self initParams];
		[self setup];
		
		NSAssert( _tex != nil , @"load failed" );
	}
	return self;
}

- (id) initWithContentsOfFile: (NSString *)path objTag:(GLuint)tag {
	
	if( ![self initWithContentsOfFile:path] ) {
		[self release];
		return nil;
	}
	_tag = tag;
	return self;
}

-(id) initWithTexture : (Texture2D *)tex {
	
	if( self = [super init] ) {
		self.texture = tex;
		[self initParams];
	}
	return self;
}

- (id) initWithTexture : (Texture2D *)tex objTag:(GLuint)tag {
	if( ![self initWithTexture:tex] ) {
		[self release];
		return nil;
	}
	_tag = tag;
	return self;
}

- (void) update {
}

- (void) draw {
	
	glPushMatrix();
	
	// 移動
	glTranslatef( _vec.x, _vec.y, _vec.z );
	
	// 回転
	glRotatef( _rot.x, 1.0f, 0.0f, 0.0f);
	glRotatef( _rot.y, 0.0f, 1.0f, 0.0f);
	glRotatef( _rot.z, 0.0f, 0.0f, 1.0f);
	
	// スケール( zは、考慮しません )
	glScalef( _scale.width, _scale.height, 1.0f);
	
	glEnable( GL_TEXTURE_2D );
	glEnable( GL_BLEND );
	
	[TextureUtils bindTexture2D:_tex];
	
	[TextureUtils setTextureBlend:_blend_s blendDst:_blend_d];
	[TextureUtils setTextureMagFilter:_magfilter];
	[TextureUtils setTextureMinFilter:_minfilter];
	[TextureUtils setTextureWrapS:_wrap_s];
	[TextureUtils setTextureWrapT:_wrap_t];
	[TextureUtils setTextureUVOffset:CGPointMake(_offs.x,_offs.y)];
	
	[DrawUtils drawWithTexture3D:_vec imageAnchor:_anchor imageRotate:_rot imageAlpha:_alpha imageScale:_scale texture:_tex isPVRTC:self.pvr];

	glDisable( GL_TEXTURE_2D );
	glDisable( GL_BLEND );

	glPopMatrix();
}

#pragma mark -
#pragma mark delegate

- (void) destroy
{
}

-(void) dealloc
{
	[self destroy];
	[_tex release];
	[super dealloc];
}

@end